//
//  WebViewController.m
//  Nerdfeed
//
//  Created by Rafal Rebacz on 7/10/13.
//  Copyright (c) 2013 Rafal Rebacz. All rights reserved.
//

#import "WebViewController.h"
#import "RSSItem.h"

@implementation WebViewController

- (void)loadView
{
    CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
    UIWebView *wv = [[UIWebView alloc] initWithFrame:screenFrame];
    
    [wv setScalesPageToFit:YES];
    
    
    _backBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(backTapped)];
    
    _forwardBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(forwardTapped)];
    
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, screenFrame.size.width, 50.0f)];
    [toolbar setAutoresizingMask:(UIViewAutoresizingFlexibleWidth)];
    [toolbar setItems:@[_backBtn, _forwardBtn]];
    
    [wv addSubview:toolbar];
    [self setView:wv];
    
    [wv setDelegate:self];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    BOOL canGoBack = [((UIWebView *)[self view]) canGoBack];
    [self setBackEnabled:canGoBack];
    
    BOOL canGoForward = [((UIWebView *)[self view]) canGoForward];
    [self setForwardEnabled:canGoForward];
}

- (void)backTapped
{
    [((UIWebView *)[self view]) goBack];
}

- (void)forwardTapped
{
    [((UIWebView *)[self view]) goForward];
}

- (UIWebView *)webView
{
    return (UIWebView *)[self view];
}

- (void)setBackEnabled:(BOOL)backEnabled
{
    _backEnabled = backEnabled;
    _backBtn.enabled = _backEnabled;
}

- (void)setForwardEnabled:(BOOL)forwardEnabled
{
    _forwardEnabled = forwardEnabled;
    _forwardBtn.enabled = _forwardEnabled;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
        return YES;
    
    return toInterfaceOrientation == UIInterfaceOrientationPortrait;
}

- (void)listViewController:(ListViewController *)lvc handleObject:(id)object
{
    RSSItem *entry = object;
    
    if(![entry isKindOfClass:[RSSItem class]])
        return;
    
    NSURL *url = [NSURL URLWithString:[entry link]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [[self webView] loadRequest:request];
    
    [[self navigationItem] setTitle:[entry title]];
}

- (void)splitViewController:(UISplitViewController *)svc
    willHideViewController:(UIViewController *)aViewController
         withBarButtonItem:(UIBarButtonItem *)barButtonItem
      forPopoverController:(UIPopoverController *)pc
{
    [barButtonItem setTitle:@"List"];
    [[self navigationItem] setLeftBarButtonItem:barButtonItem];
}

- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    if(barButtonItem == [[self navigationItem] leftBarButtonItem])
    {
        [[self navigationItem] setLeftBarButtonItem: nil];
    }
}

@end
